if (window.plus) {
  plusReady();
} else {
  document.addEventListener('plusready', plusReady, false);
}
$(function () {
  if (AGENT_CLIENT.WEB) {
      plusReady();
  } else if (AGENT_CLIENT.APP) {
      $(".main").css("padding-bottom", "2px");
  }
});

function plusReady() {
    var _page = null;

    var loginPage = function () {
        this.init();
    };
    loginPage.prototype = {
        init: function () {
            this.bindEvent();
        },
        //绑定事件
        bindEvent: function () {
          //点击回退
          $("#back").on("click", function(){
            back();
          });
          
            //点击注册
            $("#register").on("click", function () {
                redirect("register.html");
                return false;
            });

            //点击找回密码
            $("#forgetPass").on("click", function () {
                redirect('validatePhone.html');
            });

            $("#username,#password").on("input", function () {
                if ("" == $("#username").val() ||
                    "" == $("#password").val()) {
                    $("#login").removeClass("liang").unbind();
                } else {
                    //点击登陆
                    $("#login").addClass("liang").unbind().on("click", function () {

                        if (!isPhone($("#username").val())) {
                            $.alert("用户名是手机号");
                            return false;
                        }


                        $.getScript("http://pv.sohu.com/cityjson?ie=utf-8").done(function () {
                            var postData = {
                                "username": $("#username").val()/* "tony@iapp.hk"*/,
                                "password": $("#password").val() /*"123456"*/,
                                "ip": returnCitySN.cip
                            };
                            $.ajax({
                                type: "POST",
                                url: CONFIG_MANAGER.RequstUrl + "v1/account/login",
                                async: true,
                                data: postData,
                                beforeSend: function () {
                                    $.showWaiting();
                                }
                            }).done(function (data) {
                                $.closeWaiting();
                                //保存会员信息
                                removeLocalStorage(STORAGE_MANAGER.user);
                                setLocalStorage(STORAGE_MANAGER.user, data);
                                $.alert("登录成功", function () {
                                  if( window.plus ){
                                    plus.webview.currentWebview().close();
                                  }else{
                                    redirect(document.referrer);
                                  }
                                  return false;
                                });
                            }).fail(function (data) {
                                $.alert("登录失败");
                            }).always(function () {
                                $.closeWaiting();
                            });
                        });
                    });
                }
            });


        },

    };
    _page = new loginPage();
}

